#include "stdafx.h"
#include "CommonDef.h"
#include "ChessBoardView.h"

HRESULT ChessBoardView::DrawChessBoard(const HWND& hwnd)
{
    HPEN hpen = CreatePen(PS_SOLID, 1, RGB(0x0, 0x0, 0x0));
    HDC hdc = GetDC(hwnd);
    HGDIOBJ old_pen = SelectObject(hdc, hpen);

    for (int index = 0; index <= GC_CHESS_BOARD_ROW_COUNT; ++index)
    {
        POINT start_point;
        POINT end_point;
        start_point.x = 0;
        start_point.y = GC_CHESS_PIECE_HEIGHT * index;
        end_point.x = GC_CHESS_BOARD_COL_COUNT * GC_CHESS_PIECE_WIDTH;
        end_point.y = start_point.y;
        RETURN_AND_RECORD_ERROR_MESSAGE_IF_FAILED(DrawLine(hdc, start_point, end_point))
    }

    for (int index = 0; index <= GC_CHESS_BOARD_COL_COUNT; ++index)
    {
        POINT start_point;
        POINT end_point;
        start_point.x = GC_CHESS_PIECE_WIDTH * index;
        start_point.y = 0;
        end_point.x = start_point.x;
        end_point.y = GC_CHESS_BOARD_ROW_COUNT * GC_CHESS_PIECE_HEIGHT;
        RETURN_AND_RECORD_ERROR_MESSAGE_IF_FAILED(DrawLine(hdc, start_point, end_point))
    }

    SelectObject(hdc, old_pen);
    ReleaseDC(hwnd, hdc);

    return S_OK;
}

HRESULT ChessBoardView::DrawLine(const HDC& hdc, const POINT& start_point, const POINT& end_point)
{
    RETURN_AND_RECORD_ERROR_MESSAGE_IF_FALSE(MoveToEx(hdc, start_point.x, start_point.y, NULL))
    RETURN_AND_RECORD_ERROR_MESSAGE_IF_FALSE(LineTo(hdc, end_point.x, end_point.y))

    return S_OK;
}